package io.github.source;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;

import com.google.gson.Gson;

import java.lang.ref.WeakReference;

/**
 * @ClassName: App
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author: huangrongcai
 * @date: 2024/6/4
 * @Copyright: 黄嵘才（HuangRc)
 */
public final class App {

    private WeakReference<Context> context;
    private Gson gson = new Gson();
    private final Handler handler;

    public App() {
        handler = new Handler(Looper.getMainLooper());
        gson = new Gson();
    }

    public static Gson json() {
        //优化静态变量
        return get().gson;
    }

    private static class Loader {
        static volatile App INSTANCE = new App();
    }

    private static App get() {
        return Loader.INSTANCE;
    }

    private boolean hook;

    public static void setHook(boolean hook) {
        get().hook = hook;
    }

    public static void post(Runnable runnable) {
        get().handler.post(runnable);
    }

    public static void post(Runnable runnable, long delayMillis) {
        get().handler.removeCallbacks(runnable);
        if (delayMillis >= 0) get().handler.postDelayed(runnable, delayMillis);
    }

    public static void init(Context context) {
        get().context = new WeakReference<>(context);
    }

    public static Context context() {
        return get().context.get();
    }

    public static Context getInstance() {
        return get().context.get();
    }

}
